//
//  YDPropertyController.m
//  YDBASE
//
//  Created by rowena on 2019/10/17.
//  Copyright © 2019 wuyezhiguhun. All rights reserved.
//

#import "YDPropertyController.h"
#import "YDPropertyPerson.h"

@interface YDPropertyController ()

@property (nonatomic, strong) YDPropertyPerson *person;

@property (nonatomic, strong) NSString *strongStr;
@property (nonatomic, weak) NSString *weakStr;

@property (nonatomic, strong) YDPropertyPerson *strongPerson;
@property (nonatomic, weak) YDPropertyPerson *weakPerson;

@end

@implementation YDPropertyController

- (void)dealloc {
    //移除观察者
    NSLog(@"wuyezhiguhun -->: YDPropertyController -->: dealloc ");
    
    [self.person removeObserver:self forKeyPath:@"name"];
    [self.person removeObserver:self forKeyPath:@"age"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.person = [[YDPropertyPerson alloc] init];
    
//    YDPropertyPerson *person = [[YDPropertyPerson alloc] init];
//    YDPropertyPerson *propertyPerson = [[YDPropertyPerson alloc] init];
//    propertyPerson.name = @"赵钱孙";
//
//    [person setValue:@"张二蛋" forKey:@"name"];
////    [YDPropertyPerson setValue:@"嬴刘" forKeyPath:@"@propertyPerson.name"];
//
//    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: person -->: name: %@", person.name);
//    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: propertyPerson -->: name: %@", propertyPerson.name);
    
    [self.person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    [self.person addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//    [self addObserver:self.person forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    self.strongStr = @"天王盖地虎";
    self.weakStr = self.strongStr;
    self.strongStr = nil;
    
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: strongStr: %@", self.strongStr);
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: weakStr: %@ ", self.weakStr);
    NSLog(@"");
    
    self.strongPerson = [[YDPropertyPerson alloc] init];
    self.weakPerson = self.strongPerson;
    self.strongPerson = nil;
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: strongPerson: %@", self.strongPerson);
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: weakPerson: %@ ", self.weakPerson);
    NSLog(@"");
    
    self.strongPerson = [[YDPropertyPerson alloc] init];
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: strongPerson -->: %@", self.strongPerson);
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: weakPerson -->: %@ ", self.weakPerson);
    NSLog(@"");
    
    self.weakPerson = self.strongPerson;
    self.weakPerson = nil;
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: strongPerson ==>: %@", self.strongPerson);
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: weakPerson ==>: %@ ", self.weakPerson);
    NSLog(@"");
    
    self.weakPerson = self.strongPerson;
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: strongPerson **>: %@", self.strongPerson);
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: viewDidLoad -->: weakPerson **>: %@ ", self.weakPerson);
    NSLog(@"");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
//    self.person.name = @"张大傻";
//    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: TouchesBegan -->: name: %@", self.person.name);
    
    self.person.age = @"18";
    
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: TouchesBegan -->: age: %@", self.person.age);
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
//    self.person.name = @"张二傻";
//    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: TouchesEnded -->: name: %@", self.person.name);
    
    self.person.age = @"22";
    
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: TouchesEnded -->: age: %@", self.person.age);
    
}

/**
 * KVO属性监听回调
 */
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey,id> *)change
                       context:(void *)context {
    
    NSLog(@"wuyezhiguhun -->: YDPropertyPerson -->: ObserveValueForKeyPath -->: object: %@", object);
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
